import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.text.*;


public class icsGUI {
        private static JTextField textEvent;
        private static JTextField textLocation;
        private static JTextField textResource;
        private static final ButtonGroup buttonGroup = new ButtonGroup();
        
        static String event;
        static String location;
        static String comments;
        static String rClass;
        static String resource;

        /**
         * @param args
         * GUI for the calendar project
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
               
                final JFrame frame = new JFrame();
                frame.setSize(440,740);
                frame.setTitle("ICS Calendar Project");
               
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(null);
               
                JPanel panel = new JPanel();
                panel.setBounds(0, 0, 440, 718);
                frame.getContentPane().add(panel);
                panel.setLayout(null);
               
                JLabel lblCreateCalendarFile = new JLabel("Create Calendar File");
                lblCreateCalendarFile.setBounds(137, 5, 125, 16);
                panel.add(lblCreateCalendarFile);
               
                //Event
                JLabel lblName = new JLabel("Event:");
                lblName.setBounds(23, 47, 61, 16);
                panel.add(lblName);
                textEvent = new JTextField();
                textEvent.setBounds(96, 41, 298, 28);
                panel.add(textEvent);
                textEvent.setColumns(10);
               
                //Radio buttons for public and private
                final JRadioButton radioPublic = new JRadioButton("Public");
                buttonGroup.add(radioPublic);
                radioPublic.setBounds(106, 70, 141, 23);
                panel.add(radioPublic);
                final JRadioButton radioPrivate = new JRadioButton("Private");
                buttonGroup.add(radioPrivate);
                radioPrivate.setBounds(232, 70, 141, 23);
                panel.add(radioPrivate);
               
                //Location
                JLabel lblLocation = new JLabel("Location:");
                lblLocation.setBounds(23, 115, 61, 16);
                panel.add(lblLocation);
                textLocation = new JTextField();
                textLocation.setBounds(96, 109, 298, 28);
                panel.add(textLocation);
                textLocation.setColumns(10);
               
                //Resources
                JLabel lblResources = new JLabel("Resources:");
                lblResources.setBounds(23, 170, 74, 16);
                panel.add(lblResources);
                textResource = new JTextField();
                textResource.setBounds(96, 164, 298, 28);
                panel.add(textResource);
                textResource.setColumns(10);
               
                //Recurring Events
                JLabel lblRecurring = new JLabel("Recurring:");
                lblRecurring.setBounds(23, 234, 74, 16);
                panel.add(lblRecurring);
                JComboBox comboBox_12 = new JComboBox();
                comboBox_12.setModel(new DefaultComboBoxModel(new String[] {"Daily", "Weekly", "Monthly"}));
                comboBox_12.setBounds(96, 230, 298, 27);
                panel.add(comboBox_12);
                
                //Start date/time
                JLabel lblStart = new JLabel("Start:");
                lblStart.setBounds(23, 295, 61, 16);
                panel.add(lblStart);
                JLabel lblDatetime = new JLabel("Date/Time");
                lblDatetime.setBounds(23, 313, 74, 16);
                panel.add(lblDatetime);
                //Date/Time start mo
                final JComboBox comboBox = new JComboBox();
                comboBox.setModel(new DefaultComboBoxModel(new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
                comboBox.setBounds(96, 291, 131, 27);
                panel.add(comboBox);            
                //Date/Time start day
                final JComboBox comboBox_1 = new JComboBox();
                comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
                comboBox_1.setBounds(232, 291, 61, 27);
                panel.add(comboBox_1);
                //Date/Time start yr
                final JComboBox comboBox_2 = new JComboBox();
                comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026"}));
                comboBox_2.setBounds(305, 291, 89, 27);
                panel.add(comboBox_2);
                //Date/Time start hr
                final JComboBox comboBox_3 = new JComboBox();
                comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}));
                comboBox_3.setBounds(153, 320, 74, 27);
                panel.add(comboBox_3);
                //Date/Time start min
                final JComboBox comboBox_4 = new JComboBox();
                comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"}));
                comboBox_4.setBounds(232, 320, 74, 27);
                panel.add(comboBox_4);
                //Date/Time start am pm
                final JComboBox comboBox_10 = new JComboBox();
                comboBox_10.setModel(new DefaultComboBoxModel(new String[] {"am", "pm"}));
                comboBox_10.setBounds(315, 320, 79, 27);
                panel.add(comboBox_10);
               
                //End date/time
                JLabel lblEnd = new JLabel("End:");
                lblEnd.setBounds(23, 381, 61, 16);
                panel.add(lblEnd);              
                JLabel lblDatetime_1 = new JLabel("Date/Time");
                lblDatetime_1.setBounds(23, 399, 74, 16);
                panel.add(lblDatetime_1);
                //Date/Time end mo
                final JComboBox comboBox_5 = new JComboBox();
                comboBox_5.setModel(new DefaultComboBoxModel(new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
                comboBox_5.setBounds(96, 388, 131, 27);
                panel.add(comboBox_5);
                //Date/Time end day
                final JComboBox comboBox_6 = new JComboBox();
                comboBox_6.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
                comboBox_6.setBounds(232, 388, 61, 27);
                panel.add(comboBox_6);
                //Date/Time end yr
                final JComboBox comboBox_7 = new JComboBox();
                comboBox_7.setModel(new DefaultComboBoxModel(new String[] {"2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026"}));
                comboBox_7.setBounds(305, 388, 89, 27);
                panel.add(comboBox_7);
                //Date/Time end hr
                final JComboBox comboBox_8 = new JComboBox();
                comboBox_8.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}));
                comboBox_8.setBounds(153, 417, 74, 27);
                panel.add(comboBox_8);
                //Date/Time end min
                final JComboBox comboBox_9 = new JComboBox();
                comboBox_9.setModel(new DefaultComboBoxModel(new String[] {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"}));
                comboBox_9.setBounds(232, 417, 74, 27);
                panel.add(comboBox_9);
                //Date/Time end am pm
                final JComboBox comboBox_11 = new JComboBox();
                comboBox_11.setModel(new DefaultComboBoxModel(new String[] {"am", "pm"}));
                comboBox_11.setBounds(315, 417, 79, 27);
                panel.add(comboBox_11);
               
                //Description
                JLabel lblDescription = new JLabel("Comments:");
                lblDescription.setBounds(23, 488, 84, 16);
                panel.add(lblDescription);
                final JTextArea textComment = new JTextArea();
                textComment.setBounds(106, 488, 276, 143);
                panel.add(textComment);
               
                //Generate file button
                JButton btnGenerateFile = new JButton("Generate File");
                btnGenerateFile.addActionListener(new ActionListener() {
                	public void actionPerformed(ActionEvent e) {
                			event = textEvent.getText();
                			location = textLocation.getText();
                			comments = textComment.getText();
                			if (radioPublic.isSelected()) {
                				rClass = "PUBLIC";
                			}
                			if (radioPrivate.isSelected()) {
                				rClass = "PRIVATE";
                			}
                			resource = textResource.getText();
                			
                			String startMonth = (String) comboBox.getSelectedItem();
                			String startDay = (String) comboBox_1.getSelectedItem();
                			String startYear = (String) comboBox_2.getSelectedItem();
                			String startHour = (String) comboBox_3.getSelectedItem();
                			String startMin = (String) comboBox_4.getSelectedItem();
                			String startTOD = (String) comboBox_10.getSelectedItem();
                			
                			String endMonth = (String) comboBox_5.getSelectedItem();
                			String endDay = (String) comboBox_6.getSelectedItem();
                			String endYear = (String) comboBox_7.getSelectedItem();
                			String endHour = (String) comboBox_8.getSelectedItem();
                			String endMin = (String) comboBox_9.getSelectedItem();
                			String endTOD = (String) comboBox_11.getSelectedItem();
                			
                			if(errorCheck.validateDate(startMonth, startDay, startYear) == false){
                				
                				JOptionPane.showMessageDialog(frame, "You entered an invalid start date: "+startMonth+" "+ startDay+
                						" "+ startYear+". Please try again.");
                			}
                			if(errorCheck.validateDate(endMonth, endDay, endYear) == false){
                				
                				JOptionPane.showMessageDialog(frame, "You entered an invalid end date: "+endMonth+" "+ endDay+
                						" "+ endYear+". Please try again.");
                			}
                			
                			if(errorCheck.validateEvent(startMonth, startDay, startYear, startHour, startMin, startTOD, endMonth, endDay, endYear, endHour, endMin, endTOD) == false){
                				
                				JOptionPane.showMessageDialog(frame, "Your event start date/time occurs after your end date/time. Please fix and try again.");
                			}
                			
                			
                			String finalStartDateTime = errorCheck.formatDateTime(startYear, startMonth, startDay, startHour, startMin, startTOD);
                			String finalEndDateTime = errorCheck.formatDateTime(endYear, endMonth, endDay, endHour, endMin, endTOD);
                			
                			
                			
                			BufferedWriter outputStream;  
                	        String content = "";

                	        try{
                	            outputStream = new BufferedWriter(new FileWriter(new File("test.ics")));
                	           
                	            content =
                	                   "BEGIN:VCALENDAR\n" +
                	                   "VERSION:2.0\n" +
                	                   "X-WR-TIMEZONE:Pacific/Honolulu\n" + //will need to be change to the time zone specification required 'TZID'
                	                   "BEGIN:VEVENT\n" +
                	                   "DTSTART:" + finalStartDateTime + "\n" +
                	                   "DTEND:" + finalEndDateTime + "\n" +
                	                   "CLASS: " + rClass + "\n" +
                	                   "DESCRIPTION: " + comments + "\n" +
                	                   "LOCATION:" + location + "\n" +
                	                   "SUMMARY:"+ event + "\n" +
                	                   "RESOURCE:" + resource + "\n" + 
                	                   "END:VEVENT\n" +
                	                   "END:VCALENDAR\n";
                	           
                	            outputStream.write(content);
                	            outputStream.close();
                	            
                	        }
                	        catch(IOException ie){
                	           System.out.print("Unable to create file.");
                	    	}
                	}
                });
                btnGenerateFile.setBounds(137, 643, 117, 29);
                panel.add(btnGenerateFile);
                
                frame.setVisible(true);
                                        
        }
}
